#include<iostream>
#include<stdlib.h> 
#include<algorithm>
using namespace std;

typedef struct point
{
    int dis;
    int cost;        
}tPoint ;

tPoint p[10001];

/* sort by dis*/
int cmp_dis(const void *a , const void *b)
{
    return ( ((tPoint*)a)->dis ) - ( ((tPoint*)b)->dis );
}

/* sort by cost*/
int cmp_cost(const void *a , const void *b)
{
    return ( ((tPoint*)a)->cost ) - ( ((tPoint*)b)->cost );
}

int main()
{
    int n , d , c , i , j ,k , x ,count;     
    bool flag;
    while(cin>>n)
    {
        if( n == 0 )    break;
        int map[10001]={0};
        count = 0;
        for( i = 0 ; i < n ; i++ )
             cin>>p[i].dis>>p[i].cost;
        
        qsort( p,n,sizeof(tPoint),cmp_dis );//sort by dis     
        /*traverse to find the second smallest dis*/
        x=p[0].dis;
        k=0;
        while(x==p[k].dis)
            k++;
        /******************************************/
        for( i = k ; i < n ; i++ )
        {  
           flag = true;
           for( j = 0 ; j < i ;j++ ) 
           {
                if( p[j].cost <= p[i].cost  ) { flag = false;   break; }
           }
           if( flag )   
           { 
               if( map[ p[i].dis * p[i].cost % 7 ] == 0 ) 
               {
                   count++; 
                   map[ p[i].dis * p[i].cost % 7 ] == 1;
               } 
           }
        }
        
        qsort( p,n,sizeof(tPoint),cmp_cost);//sort by cost
        /*traverse to find the second smallest cost*/
        x=p[0].cost;
        k=0;
        while(x==p[k].cost)
            k++;
        /*******************************************/
        for( i = k ; i < n ; i++ )
        {  
           flag = true;
           for( j = 0 ; j < i ;j++ ) 
           {
                if( p[j].dis <= p[i].dis  ) { flag = false ; break; }
           }
           if( flag )   
           { 
               if( map[ p[i].dis * p[i].cost % 7 ] == 0 ) 
               {
                   count++; 
                   map[ p[i].dis * p[i].cost % 7 ] == 1;
               } 
           }
        }
        cout<<count<<endl;
    }
    //system("pause");
    return 0;
} 


